﻿using BookManageSystem;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using User_subsystem;

namespace web_search
{
    public partial class FormUser : Form
    {
        public FormUser()
        {
            InitializeComponent();
        }
        public static string name;
        public static string id;
        public static string address;
        public static string phone;

        public static string status = FormLogin.status;

        private void 退出登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //给用户提示是否退出
            if (DialogResult.Yes == MessageBox.Show("确认退出吗？", "退出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                this.Close();
            }
        }

        public static int havewilllate = 0;//有即将逾期也没还的书，默认为没有
        public static int havelated = 0; //有已经逾期的书，默认为没有
        private void FormUser_Load(object sender, EventArgs e)
        {
            this.label1.Text = $"欢迎您，用户：{FormLogin.name}  {FormLogin.id}";

            DaoAdmin dao = new DaoAdmin();
            dao.connect();
            string sql = $"select  UserName,Address,ContactNum" +
                $" from [UserInfor] where UserID ='{FormLogin.id}' ";
            SqlDataReader reader = dao.read(sql);
            reader.Read();
            name = reader[0].ToString();
            id = FormLogin.id;
            address = reader[1].ToString();
            phone = reader[2].ToString();
            reader.Close();
            dao.DaoClose();

        }





        private void 公告ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //点击后跳转到图书查询窗口
            FormUserSearch form = new FormUserSearch();
            form.ShowDialog();
        }

        private void 公告ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
        }

        private void 图书查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //点击后跳转到图书查询窗口
            FormUserSearch form = new FormUserSearch();
            form.ShowDialog();
        }

        private void 新书通报ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void 个人信息管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 修改个人信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DaoAdmin dao = new DaoAdmin();
            dao.connect();
            string sql = $"select  UserID,UserName,ContactNum,Address " +
                $" from [UserInfor]  where  [UserID] ='{FormLogin.id}' ";
            SqlDataReader reader = dao.read(sql);
            reader.Read();

            id = reader[0].ToString();
            name = reader[1].ToString();   
            phone = reader[2].ToString();
            address = reader[3].ToString();

            reader.Close();
            dao.DaoClose();
            FormUserselfmodify form = new FormUserselfmodify();
            form.ShowDialog();
        }

        private void 注销账号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //删除数据库中当前账号信息
            if (DialogResult.Yes == MessageBox.Show("确认注销吗？", "消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                //删除操作
                //获取到要注销的账号
                string id = FormLogin.id;
                Daouser dao = new Daouser();
                dao.connect();
                string sql = $"delete [UserInfor] where [UserID]='{id}'";
                if (dao.Execute(sql) > 0)
                {
                    //注销成功
                    MessageBox.Show("注销成功", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dao.DaoClose();
                    //返回到登录窗口
                    this.Close();
                }
                else
                {
                    //注销失败
                    MessageBox.Show("注销失败", "消息", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    dao.DaoClose();
                }
            }
        }

        private void 修改密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormUpdatePwdUser form = new FormUpdatePwdUser();
            form.ShowDialog();
        }

        private void 借阅图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 预约图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //预约图书
            FormBookBorrowForUser form = new FormBookBorrowForUser();
            form.ShowDialog();
        }

        private void 借阅图书ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //借阅图书
            FormBookBorrowForUser form = new FormBookBorrowForUser();
            form.ShowDialog();
        }

        private void 归还图书ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //还书功能
            FormReturnBook form = new FormReturnBook();
            form.ShowDialog();
        }

        private void linkLabel1_LinkClicked(object sender, EventArgs e)
        {
           
        }

        private void 借阅图书ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {

        }

        private void 刷新页面ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //个人信息修改后要修改主页展示
            DaoAdmin dao= new DaoAdmin();
            dao.connect();

            string sql = $"select UserName from UserInfor where UserID={FormLogin.id}";
            SqlDataReader reader=dao.read(sql);
            if (reader.Read())
            {
                string newname = reader[0].ToString().Trim();
                this.label1.Text = $"欢迎您，用户：{newname}  {FormLogin.id}";
            }

            MessageBox.Show("刷新成功！", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
